package com.xxl.job.executor.service.jobhandler;

import cn.hutool.core.bean.BeanUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.executor.dao.QxWarningMapper;
import com.xxl.job.executor.dao.ReservoirDataSynMapper;
import com.xxl.job.executor.model.QxWarning;
import com.xxl.job.executor.model.ReservoirDataSyn;
import com.xxl.job.executor.utils.CmUtil;
import com.xxl.job.executor.utils.ConstUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.HashMap;
import java.util.List;
/*
"同步青岛数据库（QX_WARNING、RESERVOIR_DATA_SYN）";
 */

@Component
public class SyncDataTask {
    private static Logger logger = LoggerFactory.getLogger(SyncDataTask.class);

    private String taskContents;
    @Autowired
    private ConstUtil constUtil;
    @Autowired
    private QxWarningMapper qxWarningMapper;

    private ReservoirDataSynMapper reservoirDataSynMapper;

    @XxlJob(value = "SyncDataTask",init = "init", destroy = "destroy")
    public ReturnT<String> SyncDataTask(String param) throws Exception {
        try{
//            if (qxWarningMapper == null){
//                qxWarningMapper = SpringContextHolder.getBean(QxWarningMapper.class);
//            }
            // 获取新平台预警数据
            List<QxWarning> qxWarningList = qxWarningMapper.selectWarningData();
            HashMap<String, QxWarning> localMap = new HashMap<>();
            for (QxWarning r : qxWarningList){
                if (r.getOriginalId()!=null){
                    localMap.put(r.getOriginalId(), r);
                }
            }
            // 获取青岛数据库预警信息
            qxWarningList = qxWarningMapper.selectAllWarningData();
            for(QxWarning r : qxWarningList){
                // 查询青岛数据是否在新平台中
                QxWarning local = localMap.get(r.getId());
                if (local == null){
                    // 不在，将数据插入到新平台数据库中
                    QxWarning qxWarning = new QxWarning();
                    // 复制青岛数据库数据
                    BeanUtil.copyProperties(r,qxWarning);
                    qxWarning.setOriginalId(r.getId());
                    qxWarning.setId(CmUtil.getUUID());
                    qxWarningMapper.insert(qxWarning);
                    continue;
                }else if (!r.toString().equals(local.toString())){
                    r.setId(local.getId());
                    qxWarningMapper.updateByPrimaryKeySelective(r);
                }
                this.taskContents = "同步青岛数据库（QX_WARNING）数据成功！";
            }
        }catch (Exception e){
            e.printStackTrace();
            this.taskContents = "同步青岛数据库（QX_WARNING）数据异常！"+e.getMessage();
        }
        try {
//            if (reservoirDataSynMapper == null){
//                reservoirDataSynMapper = SpringContextHolder.getBean(ReservoirDataSynMapper.class);
//            }
            List<ReservoirDataSyn> list = reservoirDataSynMapper.selectAll();
            HashMap<String, ReservoirDataSyn> localMap = new HashMap<>();
            for (ReservoirDataSyn r : list){
                localMap.put(r.getSid(), r);
            }
            list = reservoirDataSynMapper.selectAllSource();
            for (ReservoirDataSyn r : list){
                ReservoirDataSyn local = localMap.get(r.getSid());
                if (local == null){
                    r.setReservoirDataId(CmUtil.getUUID());
                    reservoirDataSynMapper.insert(r);
                    continue;
                }
                ReservoirDataSyn toUpd = new ReservoirDataSyn();
                BeanUtil.copyProperties(local, toUpd);
                toUpd.setReservoirName(r.getReservoirName());
                toUpd.setWaterLevel(r.getWaterLevel());
                toUpd.setWaterRetentionCap(r.getWaterRetentionCap());
                toUpd.setCreatedTime(r.getCreatedTime());
                toUpd.setUpdatedTime(r.getUpdatedTime());
                if (!toUpd.toString().equals(local.toString())){
                    reservoirDataSynMapper.updateByPrimaryKey(toUpd);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            this.taskContents = "同步青岛数据库（RESERVOIR_DATA_SYN）数据异常！"+e.getMessage();
        }

        return ReturnT.SUCCESS;
    }

    public void init(){
        logger.info("init");
    }
    public void destroy(){
        logger.info("destory");
    }
}
